package com.campus.client.controller;

import com.campus.common.utils.uuid.UUID;
import com.campus.pojos.domain.dto.ArticleDto;
import com.campus.result.Result;
import com.campus.utils.AliOSSClientUtil;
import com.campus.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 通用接口
 */
@RestController("ClientCommonController")
@RequestMapping("/client/common")
@Slf4j
public class CommonController {


    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private AliOSSClientUtil aliOSSClientUtil;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("上传文件{}", file);
        //获得文件原始名
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        String extendName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //UUID拼接后缀生成新的文件名
        String objectName = UUID.randomUUID() + extendName;
        try {
            String uploadUrl = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(uploadUrl);
        } catch (IOException e) {
            log.error("发生错误{}",e);
        }
        return Result.error("文件上传失败");
    }


    /**
     * 视频上传
     */
    @PostMapping("uploadVideos")
    public Result<String> uploadVideo(MultipartFile file, ArticleDto articleDto, HttpServletRequest request){
        log.info("上传视频:{}",file);

        //上传阿里云
        String url = aliOSSClientUtil.uploadVideo(file);

        return Result.success(url);
    }


}
